<?
require("../../common/init.php");
require("author.php");

$gee=$customer; // gee, 意为赶车时候喊的"驾", 此处用来表示次页面的主角对象
//$cate=$language; // cate = category, 分类

$title=ROOT_CUSTOMER_MANAGEMENT;
$url='customer';
$s=isset($_GET["s"])?clean($_GET["s"]):''; // search
$do=isset($_GET["do"])?clean($_GET["do"]):'view'; // 操作: 查看/编辑/删除 
$action=isset($_POST["action"])?clean($_POST["action"]):''; // 数据库操作: CURD 
$id=0; if (isset($_GET["id"])) $id = clean($_GET["id"]);
$page=1; if(isset($_GET["page"]))if(is_numeric($_GET["page"]))$page=$_GET["page"];
$cateid=0; if (isset($_GET["cateid"])) $cateid = clean($_GET["cateid"]);

// sql action / sql 动作
switch ($action){
	case "update": sql_update(); break;
	case "add": sql_insert(); break;
}

if($id==0){
	$page=1; if(isset($_GET["page"]))if(is_numeric($_GET["page"]))$page=$_GET["page"];
	$order='id';
	$where=array();
	if($s!='') {
		$where[]=array('name'=>'last_name','val'=>$s,'opn'=>'like', 'and'=>'and');
		$where[]=array('name'=>'first_name','val'=>$s,'opn'=>'like', 'and'=>'or');
		$where[]=array('name'=>'nickname','val'=>$s,'opn'=>'like', 'and'=>'or');
		$where[]=array('name'=>'phone','val'=>$s,'opn'=>'like', 'and'=>'or');
		$where[]=array('name'=>'mobile','val'=>$s,'opn'=>'like', 'and'=>'or');
		$where[]=array('name'=>'email','val'=>$s,'opn'=>'like', 'and'=>'or');
	}
	$pages=$gee->count_pages($where);
	$info=$gee->get_page($page,$where,$order);
}
else $info=$gee->get_row($id);

function add()
{
	global $url,$h;
	$row = array();
	$h->form($url.'.php', 1, '');
	show_box($row, true, true);		
	$h->hidden('action', 'add');
	$h->former();
}
function update($info)
{
	global $gee, $url, $h;
	if($id=="") exit("Invali argument!!!");
	$h->form($url.'.php', 1, '');
  	show_box($info, true, false);
	$h->hidden('sql', 'update');
	$h->former();
}
function delete($id)
{
	global $gee,$url, $page, $cateid;;
	if($gee->delete($id)) echo "<script>location='$url-$cateid-p$page.html';</script>";
}	
function flip($id)
{
	global $gee,$url, $page, $cateid;;
	if($gee->flip($id)) echo "<script>location='$url-$cateid-p$page.html';</script>";
}	
function show_info($info)
{
	global $gee, $id;
	if($id==0) show_boxes($info); 
	else show_box($info, false, false); 
}
function sql_insert()
{
	global $gee,$url;
	$arr=array(
		'cname'=>iso(@$_POST["cname"]),
		'ccode'=>iso(@$_POST["ccode"]),
		'cyear'=>iso(@$_POST["cyear"]),
	);
	if($gee->insert($arr))
	echo '<script type="text/javascript">location="'.$page.'.php";</script>';
}
function sql_delete()
{
	global $gee,$url;
	$page=1; $page=iso(@$_POST["page"]);
	$cateid=0; $cateid=iso(@$_POST["cateid"]);
	$all=$_POST["all"];
	$sql=' where id in(0';
	if(count($all)>0)foreach($all as $a){ $sql.=','.$a; }
	$sql.='); ';
	if($gee->deletes($sql))
	echo '<script type="text/javascript">location="'.$url.'-'.$cateid.'-p'.$page.'.html";</script>';
}
function sql_update()
{
	global $gee,$url, $page, $cateid;
	$page=1; $page=iso(@$_POST["page"]);
	$cateid=0; $cateid=iso(@$_POST["cateid"]);
	$id=iso(@$_POST["id"]);
	$arr=array(
		'cname'=>iso(@$_POST["cname"]),
		'ccode'=>iso(@$_POST["ccode"]),
		'cyear'=>iso(@$_POST["cyear"]),
		);
		if($gee->update($arr, 'id', $id)) echo '<script type="text/javascript">location="'.$url.'-'.$cateid.'-p'.$page.'.html";</script>';
	}

// view 加载视图
require ROOTVIEW.$url.'.php';

// end about.php